import 'package:chewie/chewie.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import './routes.dart';
import 'dart:async';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  MyApp({Key? key}) : super(key: key);

  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
// ignore: top_level_function_literal_block
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/',
      onGenerateRoute: onGenerateRoute,
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  late VideoPlayerController _videoPlayerController;
  late ChewieController _chewieController;
  int currentIndex = 0;
  int videoIndex = 0;
  var videoArr = [
    'https://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4',
    'https://1255652068.vod2.myqcloud.com/ccb610e7vodgzp1255652068/547f3a453701925923712367832/Sybqn7HKaRMA.mp4',
    'https://1255652068.vod2.myqcloud.com/ccb610e7vodgzp1255652068/79702b9f5285890795661894383/rj8YTJYA850A.mp4',
    'https://1255652068.vod2.myqcloud.com/ccb610e7vodgzp1255652068/4eec2f9b5285890797049599501/tXyZIbqDjQYA.mp4',
    'https://1255652068.vod2.myqcloud.com/ccb610e7vodgzp1255652068/e256b7885285890801620451649/ag895lP2QD8A.mp4',
    'https://1255652068.vod2.myqcloud.com/3ba8fdcavodcq1255652068/d1c633d35285890793336911690/qdG7LxQ7U4wA.mp4',
    'https://1255652068.vod2.myqcloud.com/ccb610e7vodgzp1255652068/9779e3ed3701925922818569533/f0.mp4',
    'https://1255652068.vod2.myqcloud.com/ccb610e7vodgzp1255652068/f42d1d673701925922025359828/1woFhRYAaA0A.mp4',
    'https://1255652068.vod2.myqcloud.com/ccb610e7vodgzp1255652068/7ebf004f5285890794186450171/NvlXlr0mxJ8A.mp4'
  ];
  initVideo(index) {
    // print("我执行啦initVideo--$index");
    _videoPlayerController = VideoPlayerController.network(videoArr[index]);
    // _videoPlayerController.initialize();
    _chewieController = ChewieController(
      videoPlayerController: _videoPlayerController,
      // aspectRatio: 2.3 / 1,
      autoPlay: true,
      looping: false,
    );
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    initVideo(0);
    // _videoPlayerController = VideoPlayerController.network(videoArr[0]);
    // _chewieController = ChewieController(
    //     videoPlayerController: _videoPlayerController,
    //     // aspectRatio: 2.3 / 1,
    //     autoPlay: false,
    //     looping: false);
  }

  /*销毁*/
  @override
  void dispose() {
    super.dispose();
    _videoPlayerController.dispose();
    _chewieController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('video demo'),
      ),
      body: Listener(
        onPointerUp: (upPointEvent) {
          // print("videoIndex:$videoIndex---currentIndex:$currentIndex");
          if (videoIndex != currentIndex) {
            initVideo(currentIndex);
            this.setState(() {
              this.videoIndex = currentIndex;
            });
          }
        },
        child: PageView.builder(
          scrollDirection: Axis.vertical,
          itemCount: videoArr.length,
          onPageChanged: (index) {
            this.setState(() {
              this.currentIndex = index;
            });
          },
          itemBuilder: (context, index) {
            return Center(
              child: videoIndex == index
                  ? Chewie(controller: _chewieController)
                  : Text("哈哈哈哈不是我"),
            );
          },
        ),
      ),
    );
  }
}
// 
// Chewie(controller: _chewieController),
// https://vfx.mtime.cn/Video/2019/02/04/mp4/190204084208765161.mp4
// http://1255652068.vod2.myqcloud.com/ccb610e7vodgzp1255652068/547f3a453701925923712367832/Sybqn7HKaRMA.mp4
// http://1255652068.vod2.myqcloud.com/ccb610e7vodgzp1255652068/79702b9f5285890795661894383/rj8YTJYA850A.mp4
// http://1255652068.vod2.myqcloud.com/ccb610e7vodgzp1255652068/4eec2f9b5285890797049599501/tXyZIbqDjQYA.mp4
// http://1255652068.vod2.myqcloud.com/ccb610e7vodgzp1255652068/e256b7885285890801620451649/ag895lP2QD8A.mp4

